package com.kehutong.house.admin;

import com.kehutong.common.excel.tool.ExcelTool;

import com.kehutong.house.entity.Community;
import com.kehutong.house.entity.SupplierGoods;

import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;

import org.coraframework.orm.exception.ORMException;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;



/**
 * TODO 供应商物品管理
 *
 * @author zwp
 * @version 1.0
 * @date 2021/6/29 16:41
 */
@MvcAction
@ReqMapping("/admin/basic/supplier/goods")
public class SupplierGoodsController {
    @Inject
    private JdbcSession jdbcSession;


    @Inject
    ExcelTool excelTool;
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) throws Exception {
        FindPage<SupplierGoods> findPage = jdbcSession.findPage(SupplierGoods.class);
        findPage.eq("deleted",false);
        String name = jsonObject.getString("name");
        if(jsonObject.containsKey("status")&&Objects.nonEmpty(jsonObject.getString("status"))){
            boolean status = jsonObject.getBoolean("status");
            findPage.eq("status",status);
        }
        if(Objects.nonEmpty(name)){
            findPage.like("name",name);
        }
        findPage.page(jsonObject);
        Page<SupplierGoods> page = findPage.exe();
        return page;
    }
    @ReqMapping("/getDataList")
    public Object getDataList(JSONObject jsonObject) throws Exception {
        return jdbcSession.findArray(SupplierGoods.class)
                .eq("deleted", false)
                .eq("status",true)
                .exe();
    }
    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(SupplierGoods supplierGoods) throws Exception {
        // 增加校验不允许有相同的小区名称
        SupplierGoods item = jdbcSession.findOne(SupplierGoods.class)
                .eq("name", supplierGoods.getName())
                .eq("deleted", false)
                .exe();
        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前供应业物品名称，请修改后重试");
        }
        try {
            jdbcSession.insert(supplierGoods);
        } catch (ORMException e){
            return Result.error(0, "新增供应物品失败，请联系管理员！");
        }
        return Result.success(supplierGoods);
    }
    @Bean(newInstance=true, copy=true)
    @ReqMapping("/update")
    public Object update(SupplierGoods supplierGoods)throws Exception  {
        // 增加校验不允许有相同的小区名称
        SupplierGoods item = jdbcSession.findOne(SupplierGoods.class)
                .eq("name", supplierGoods.getName())
                .eq("deleted", false)
                .exe();
        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前供应业物品名称，请修改后重试");
        }
        try {
            jdbcSession.updateById(supplierGoods);
        } catch (ORMException e){
            return Result.error(0, "修改供应物品失败，请联系管理员！");
        }
        return Result.success(supplierGoods);
    }
    @Bean(newInstance=true, copy=true)
    @ReqMapping("/updateStatus")
    public Object updateStatus(SupplierGoods supplierGoods)throws Exception  {
        try {
            jdbcSession.updateById(supplierGoods);
        } catch (ORMException e){
            return Result.error(0, "修改供应物品失败，请联系管理员！");
        }
        return Result.success(supplierGoods);
    }
    @ReqMapping("/delete")
    public Object delete(SupplierGoods supplierGoods)throws Exception  {
        if (Objects.nonNull(supplierGoods)) {
            supplierGoods.setDeleted(true);
            jdbcSession.updateById(supplierGoods);
        }
        return Result.success();
    }
    @ReqMapping("/get")
    public Object get(String id) throws Exception {
        SupplierGoods supplierGoods=jdbcSession.findById(SupplierGoods.class,id);
        return supplierGoods;
    }

}
